home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / alib / csup / commodities_support / invert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-15  |  3.0 KB  |  158 lines

  1.  
  2.  
  3. #include <exec/memory.h>
  4. #include <devices/keymap.h>
  5. #include <libraries/commodities.h>
  6.  
  7. #include <clib/commodities_protos.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/alib_protos.h>
  10.  
  11. #include <pragmas/commodities_pragmas.h>
  12. #include <pragmas/exec_pragmas.h>
  13.  
  14.  
  15. extern struct Library * __far CxBase;
  16. extern struct Library * __far SysBase;
  17.  
  18.  
  19. int doangle(STRPTR *,struct InputEvent *);
  20. BYTE doesc(BYTE);
  21.  
  22.  
  23. struct InputEvent *InvertString(STRPTR str, struct Keymap *km)
  24. {
  25.    /* bugs:
  26.       can't escape '>'
  27.       puts '\0' on closing angles
  28.     */
  29.    /* allocate input event for each character in string   */
  30.  
  31.    struct InputEvent   *chain = NULL;
  32.    /* struct InputEvent   *head; */
  33.    struct InputEvent   *ie;
  34.    int               abort = 0;
  35.    UBYTE            cc;
  36.  
  37.    if (!str || !*str) return (NULL);
  38.  
  39.  
  40.    /*head = (struct InputEvent *) &chain;*/   /* jvudu   */
  41.  
  42.    do {   /* have checked that str is not null    */
  43.       /* allocate the next ie and link it in   */
  44.       if (!(ie =
  45.          AllocMem((LONG) sizeof (struct InputEvent), (LONG) MEMF_CLEAR)))
  46.       {
  47.          abort = 1;
  48.          goto DONE;
  49.       }
  50.       ie->ie_NextEvent = chain;
  51.       chain = ie;
  52.       /*
  53.       tail->ie_NextEvent = ie;
  54.       tail = ie;
  55.       */
  56.  
  57.       /* now fill in the input event   */
  58.       switch (cc = *str)
  59.       {
  60.       case   '<':
  61.          ++str;
  62.          if (!doangle(&str, ie))
  63.          {
  64.             abort = 1;
  65.             goto DONE;
  66.          }
  67.          break;
  68.  
  69.       case   '\\':
  70.          if ((cc = doesc( *(++str) )) == -1)   /* get escaped character   */
  71.          {
  72.             abort = 1;
  73.             goto DONE;
  74.          }
  75.          /* fall through   */
  76.       default:
  77.          InvertKeyMap((ULONG) cc, ie,(struct KeyMap *)km);
  78.       }
  79.  
  80.  
  81.    } while (*(++str));
  82.  
  83. DONE:
  84.    if (abort)
  85.    {
  86.       FreeIEvents(ie);
  87.       return (NULL);
  88.    }
  89.    return (chain);
  90. }
  91.  
  92. BYTE doesc(BYTE cc)
  93. {
  94.     switch(cc)
  95.     {
  96.         case   'n' :
  97.         case   'r' : return ('\r');
  98.                      break;
  99.  
  100.         case   't' : return ('\t');
  101.                      break;
  102.  
  103.         case   '0' : return ('\0');
  104.                      break;
  105.  
  106.         case   '\\':
  107.         case   '\"':
  108.         case   '\'':
  109.         case   '<' : break;
  110.  
  111.         default    : cc = -1;
  112.     }
  113.  
  114.     return (cc);
  115. }
  116.  
  117. /* return 0 if problem   */
  118. int doangle(STRPTR *strp, struct InputEvent *ie)
  119. {
  120.    IX   ix;
  121.    char   *cp;
  122.    ULONG   retval;
  123.  
  124.    /* find closing angle '>', put a null there   */
  125.    for (cp = *strp; *cp; ++cp)
  126.    {
  127.       if (*cp == '>')
  128.       {
  129.          *cp = '\0';
  130.          break;
  131.       }
  132.    }
  133.  
  134.    retval = ParseIX(*strp, &ix);
  135.  
  136.    if (cp)
  137.    {
  138.       *cp = '>';   /* fix it   */
  139.       *strp = cp;   /* point to char following '>'   */
  140.    }
  141.    else
  142.    {
  143.       *strp = cp - 1;   /* ++will point to terminating null   */
  144.    }
  145.  
  146.    if (retval)    /* error */
  147.    {
  148.       return (0);
  149.    }
  150.  
  151.    /* use IX to describe a suitable InputEvent */
  152.    ie->ie_Class     = ix.ix_Class;
  153.    ie->ie_Code      = ix.ix_Code;
  154.    ie->ie_Qualifier = ix.ix_Qualifier;
  155.  
  156.    return (1);
  157. }
  158.